<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use library\Controller;
use think\facade\Request;
use think\facade\Session;
use \app\admin\model\Goods as Goods_m;

/**
 * 商品管理
 * Class Goods
 * @package app\admin\controller
 */
class Goods extends Controller
{
    /**
     * 商品列表
     *@auth true
     *@menu true
     */
    public function goods_list()
    {
        $this->title = '商品列表';
        $keyword = input('get.keyword/s','');
        $list = Goods_m::goods_list($keyword);
        $page = $list->render();
        $this->assign('pages', $page);
        $this->assign('list',$list);
        $goods_type = Goods_m::goods_type();
        $this->assign('goods_type',$goods_type);
        return $this->fetch();
    }

    /**
     * 添加商品
     *@auth true
     */
    public function goods_add()
    {
        if (Request::isPost()){
            $goods_info['goods_name'] = Request::post('goods_name');
//            $goods_info['goods_info'] = Request::post('goods_info');
            $goods_info['goods_price'] = Request::post('goods_price');
            $goods_info['goods_pic'] = Request::post('goods_pic');
            $goods_info['type'] = Request::post('type');
            $goods_info['status'] = 1;
            $goods_info['create_time'] = time();
            $goods_add = Goods_m::goods_add($goods_info);
            switch ($goods_add)
            {
                case 'success':
                    $this->success('添加成功');
                    break;
                case 'error':
                    $this->error('添加失败');
                    break;
            }
        }
        $goods_type = Goods_m::goods_type();
        $this->assign('goods_type',$goods_type);
        return $this->fetch();
    }

    /**
     * 编辑商品
     *@auth true
     */
    public function goods_edit($id)
    {
        $id = (int)$id;
        $goods_info = Goods_m::goods_info($id);
        $this->assign('goods_info',$goods_info);
        $goods_type = Goods_m::goods_type();
        $this->assign('goods_type',$goods_type);
        if (Request::isPost()){
            $edit_info['goods_name'] = Request::post('goods_name');
            $edit_info['type'] = Request::post('type');
            $edit_info['goods_price'] = Request::post('goods_price');
            $edit_info['goods_pic'] = Request::post('goods_pic');
            $edit_info['goods_info'] = Request::post('goods_info');
            $edit_info['update_time'] = Request::post('update_time');
            $edit_info = Goods_m::goods_edit($id,$edit_info);
            switch ($edit_info)
            {
                case 'success':
                    $this->success('修改成功');
                    break;
                case 'error':
                    $this->error('修改失败');
                    break;
            }
        }
        return $this->fetch();
    }

    /**
     * 更改商品状态
     * @auth true
     */
    public function edit_goods_status()
    {
        $this->_form('xy_goods_list', 'form');
    }

    /**
     * 删除商品
     * @auth true
     */
    public function goods_del()
    {
        $this->_delete('fd_goods_list');
    }

    /**
     * 商品分类
     *@auth true
     */
    public function goods_type()
    {
        $this->title = '商品分类';
        $this->_query('fd_goods_type')->page();
    }

    /**
     * 添加分类
     *@auth true
     */
    public function goods_type_add()
    {
        if (Request::isPost()){
            $goods_type_info['name'] = Request::post('name');
            $goods_type_info['bili'] = Request::post('bili');
            $goods_type_info['cate_info'] = Request::post('cate_info');
            $goods_type_info['min'] = Request::post('min');
            $goods_type_info['status'] = 1;
            $goods_type_info['create_time'] = time();
            $goods_type_add = Goods_m::goods_type_add($goods_type_info);
            switch ($goods_type_add)
            {
                case 'success':
                    $this->success('添加成功');
                    break;
                case 'error':
                    $this->error('添加失败');
                    break;
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑商品信息
     * @auth true
     */
    public function goods_type_edit($id)
    {
        $id = (int)$id;
        $goods_type_info = Goods_m::goods_type_info($id);
        $this->assign('info',$goods_type_info);
        if (Request::isPost()){
            $edit_info['name'] = Request::post('name');
            $edit_info['bili'] = Request::post('bili');
            $edit_info['cate_info'] = Request::post('cate_info');
            $edit_info['min'] = Request::post('min');
            $edit_info['update_time'] = time();
            $goods_type_edit = Goods_m::goods_type_edit($id,$edit_info);
            switch ($goods_type_edit)
            {
                case 'success':
                    $this->success('修改成功');
                    break;
                case 'error':
                    $this->error('修改失败');
                    break;
            }
        }
        return $this->fetch();
    }

    /**
     * 删除商品分类
     * @auth true
     */
    public function goods_type_del()
    {
        if (Request::isPost()){
            $id = Request::post('id');
            $del = Goods_m::goods_type_del($id);
            switch ($del)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'删除成功'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'删除失败'];
                    break;
            }
        }
    }

    /**
     * 首页Roll
     *@auth true
     */
    public function roll_list()
    {
        $this->title = '首页Roll';
        $list = Goods_m::roll_list();
        $page = $list->render();
        $this->assign('pages', $page);
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 添加Roll
     *@auth true
     */
    public function roll_add()
    {
        if (Request::isPost()){
            $info['name'] = Request::post('name');
            $info['type'] = 1;
            $info['sign'] = Request::post('sign');
            $info['img'] = Request::post('img');
            $info['money'] = Request::post('money');
            $info['status'] = 1;
            $info['create_time'] = time();
            $roll_add = Goods_m::roll_add($info);
            switch ($roll_add)
            {
                case 'success':
                    $this->success('添加成功');
                    break;
                case 'error':
                    $this->error('添加失败');
                    break;
            }
        }
        return $this->fetch();
    }

    /**
     * 删除roll
     *@auth true
     */
    public function roll_del()
    {
        if (Request::isPost()){
            $id = Request::post('id');
            $del = Goods_m::roll_del($id);
            switch ($del)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'删除成功'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'删除失败'];
                    break;
            }
        }
    }

    /**
     * 首页News
     *@auth true
     */
    public function news_list()
    {
        $this->title = '首页News';
        $list = Goods_m::news_list();
        $page = $list->render();
        $this->assign('pages', $page);
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 添加News
     *@auth true
     */
    public function news_add()
    {
        if (Request::isPost()){
            $info['name'] = Request::post('name');
            if ($info['name'] == null) $this->error('昵称不能为空');
            if (strlen($info['name']) < 6) $this->error('昵称长度至少6位');
            $info['type'] = 2;
            $info['sign'] = 0;
            $info['img'] = Request::post('img');
            $info['money'] = Request::post('money');
            $info['status'] = 1;
            $info['create_time'] = time();
            $roll_add = Goods_m::news_add($info);
            switch ($roll_add)
            {
                case 'success':
                    $this->success('添加成功');
                    break;
                case 'error':
                    $this->error('添加失败');
                    break;
            }
        }
        return $this->fetch();
    }

    /**
     * 删除news
     *@auth true
     */
    public function news_del()
    {
        if (Request::isPost()){
            $id = Request::post('id');
            $del = Goods_m::news_del($id);
            switch ($del)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'删除成功'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'删除失败'];
                    break;
            }
        }
    }




}